﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Diagnostics;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;

using FCNS.KeepMoneyLibrary.Util;

/*
 * 记录金额更改后需要更新
 * BankField 钱袋余额
 * HealthStatus 收入-支出 合计(用于更新控件)
 */

namespace FCNS.KeepMoneyLibrary.Data
{
    public class EntryField
    {
        public void Remove()
        {
            BankField.GetBankField.Find(bf => { return bf.Id == BankId; }).SetBankBalance(-Money);
            HealthStatusUtil.RemoveEntry(this);

            KMData.DataTableEntrys.Rows.Remove(dRow);
            fields.Remove(this);
        }

        static List<EntryField> fields = new List<EntryField>();
        public static List<EntryField> GetEntryField
        {
            get
            {
                if (fields.Count == 0)
                    foreach (DataRow row in KMData.DataTableEntrys.Rows)
                        fields.Add(new EntryField(row));

                return fields;
            }
        }

        bool newEntry = false;
        /// <summary>
        /// 将 EntryField 实例写入数据库并自动更新数据库的统计,不要再做其他的统计功能.
        /// </summary>
        public void AddRowToDataTable()
        {
            if (!newEntry)
                return;

            Debug.Assert(BankField.GetBankField.Find(gbf => { return gbf.Id == this.BankId; }).Enable, "钱袋已停用");

            KMData.DataTableEntrys.Rows.Add(dRow);
            ConfigFlag();
            fields.Add(this);
            HealthStatusUtil.AddEntry(this);

            BankField.GetBankField.Find(bf =>
            {
                return bf.Id == BankId;
            }).SetBankBalance(Money);

            newEntry = false;
        }

        DataRow dRow = null;
        public DataRow Row
        {
            get { return dRow; }
        }

        public EntryField(DataRow row)
        {
            Debug.Assert(row != null, "KMDataField.EntryField(DataRow row)");

            dRow = row;
            ConfigFlag();
        }

        public EntryField(int groupId, int bankId, DateTime date, decimal money, KMDEntryType entryType)
        {
            if (money == 0)
                return;

            if (!GroupField.GetGroupType(groupId))
                if (!BankField.VerifyWhetherSufficientToPay(bankId, money))
                    return;

            newEntry = true;
            dRow = KMData.DataTableEntrys.NewRow();
            GroupId = groupId;
            BankId = bankId;
            Date = date;
            Money = money;
            EntryType = entryType;
        }

        public EntryField(int groupId)
        {
            dRow = KMData.DataTableEntrys.NewRow();
            newEntry = true;
            GroupId = groupId;
        }


        public string Mark
        {
            get { return (string)dRow["Mark"]; }
            set { dRow["Mark"] = value; }
        }

        public KMDEntryType EntryType
        {
            get { return (KMDEntryType)((int)dRow["EntryType"]); }
            set { dRow["EntryType"] = (int)value; }
        }

        public int Id
        {
            get { return (int)dRow["Id"]; ; }
        }

        public int GroupId
        {
            get
            {
                return (int)dRow["GroupId"];
            }
            set
            {
                dRow["GroupId"] = value;
            }
        }

        public int BankId
        {
            get { return (int)dRow["BankId"]; }
            set
            {
                dRow["BankId"] = value;
            }
        }

        public string columnName
        {
            get { return (string)dRow["Name"]; }
            set
            {
                dRow["Name"] = value;
            }
        }

        public DateTime Date
        {
            get { return (DateTime)dRow["Date"]; }
            set
            {
                dRow["Date"] = value;
            }
        }
        public string columnDate
        {
            get
            {
                return Date.ToString(LibDefines.dataTimeFormat);
            }
        }

        string flag = string.Empty;
        public string columnMoney
        {
            get { return flag + Math.Abs(Money).ToString(); }
        }
        /// <summary>
        /// 设置金额后会自动更新数据库的统计,不要再做其他的统计功能.
        /// </summary>
        public decimal Money
        {
            get
            {
                Debug.Assert(dRow["Money"] is decimal);

                return (decimal)dRow["Money"];
            }
            set
            {
                decimal de = value;
                if (Math.Abs(de) == Math.Abs(Money))
                    return;

                if (GroupField.GetGroupType(GroupId) && value < 0)
                    de = -de;
                else if (!GroupField.GetGroupType(GroupId) && value > 0)
                    de = -de;

                if (!newEntry)
                    HealthStatusUtil.EditEntry(this, de);

                dRow["Money"] = de;
            }
        }

        public string columnNote
        {
            get { return (string)dRow["Note"]; }

            set
            {
                dRow["Note"] = value;
            }
        }

        public string columnAlertDate
        {
            get
            {
                if (AlertDate != null)
                   return ((DateTime)AlertDate).ToString(LibDefines.dataTimeFormat);
                else
                    return string.Empty;
            }
        }
        public DateTime? AlertDate
        {
            get
            {
                if (dRow["AlertDate"] is DBNull)
                    return null;
                else
                    return (DateTime)dRow["AlertDate"];
            }
            set
            {
                if (value != null)
                    dRow["AlertDate"] = value;
                else
                    dRow["AlertDate"] = DBNull.Value;
            }
        }

        public string columnTag
        {
            get
            {
                if (dRow["Tag"] is DBNull)
                    return string.Empty;
                else
                    return (string)dRow["Tag"];
            }
            set { dRow["Tag"] = value; }
        }

        public string columnBankName
        {
            get
            {
                try { return BankField.GetBankField.Find(bf => { return bf.Id == BankId; }).Name; }
                catch
                {
                    Debug.Assert(false, "columnBankName");
                    return string.Empty;
                }
            }
        }

        public string columnGroupName
        {
            get
            {
                try
                {
                    return GroupField.GetGroupField.Find(gf => { return gf.Id == GroupId; }).Name;
                }
                catch
                {
                    Debug.Assert(false, "columnGroupName");
                    return string.Empty;
                }
            }
        }


        private void ConfigFlag()
        {
            if (Money > 0)
                flag = "+";
            else
                flag = "-";

            if ((!LibDefines.showPlus && flag == "+") || (!LibDefines.showMinus && flag == "-"))
                flag = string.Empty;
        }
    }
}
